package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.bean.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 获取用户所有地址
     * @return
     */
    @Override
    public List<AddressBook> list() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        //筛选出当前用户的地址簿 按照是否默认和更新时间降序排列
        queryWrapper.eq(AddressBook::getUserId, BaseContext.get())
                    .orderByDesc(AddressBook::getIsDefault)
                    .orderByDesc(AddressBook::getUpdateTime);
        return addressBookMapper.selectList(queryWrapper);
    }

    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @Override
    public int addAddress(AddressBook addressBook) {

        //给地址设置用户id
        addressBook.setUserId(BaseContext.get());
        return addressBookMapper.insert(addressBook);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @Override
    public int updateAddress(AddressBook addressBook) {
        return addressBookMapper.updateById(addressBook);
    }

    /**
     * 删除地址
     *
     * @param addressId
     * @return
     */
    @Override
    public int deleteAddress(Long addressId) {
        return addressBookMapper.deleteById(addressId);
    }

    /**
     * 查询单个地址
     *
     * @param addressId
     * @return
     */
    @Override
    public AddressBook addressFindOne(Long addressId) {
        return addressBookMapper.selectById(addressId);
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @Override
    public int setDefaultAddress(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();

        //将该用户地址簿所有地址默认值归0
        updateWrapper.eq(AddressBook::getUserId,BaseContext.get())
                    .set(AddressBook::getIsDefault,0);
        addressBookMapper.update(null,updateWrapper);

        //然后将要修改的地址对象设置默认值为1
        addressBook.setIsDefault(1);

        //最后修改地址
        return addressBookMapper.updateById(addressBook);
    }

    /**
     * 获取默认地址
     *
     * @return
     */
    @Override
    public AddressBook getDefaultAddress() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        //设置查询条件： 1.当前用户 2.默认值为1
        queryWrapper.eq(AddressBook::getUserId,BaseContext.get())
                    .eq(AddressBook::getIsDefault,1);
        return addressBookMapper.selectOne(queryWrapper);
    }

}
